<?php
namespace App\Models\Collect;
use App\Libs\Base\Constant;
use App\Models\Goods\Goods;
use App\Models\Diary\Diary;

class Collect extends \App\Libs\Base\BaseModel
{
    protected $__TABLE__ = 't_paile_collect';
	
	/**
	*	跳过检测了
	*/
	public function beforeValidation()
	{
	  //self::skipValidation([] , $this );
	}
	
	/**
	*	获取收藏的信息
	*	param $uid 用户id
	*	param $type 收藏type类型： 1.商品 2.日志 3.体验商品
	*/
    public static function getCollectInfo( $uid , $type )
    {
		$result = self::findAll( array(
			' uid = :uid: and type = :type:',
			'bind' => array( 'uid'=>$uid , 'type'=>$type )
		) );
		//获取收藏的信息id
		$gidArr = [];
		foreach( $result  as $k => $v ){
			$gidArr[] = $v->cid;
		}
		if( empty($gidArr) ){
			return [];
		}
	//	$gidStr = implode( ',' , $gidArr );
		if( $type == 1 or $type == 3 ){
			//商品收藏 
			$info = Goods::findAll( array(
				'id IN ({gid:array}) ',
				'bind'=> array( 'gid'=>$gidArr )
			) );
			if( empty( $info ) ){
				$info = [];
			}else{
				$info = $info->toArray();
			}
			foreach( $info as $k=>$v ){
				if( $v['show_status'] == 1 ){
					$info[$k]['show_status_value'] = '已失效';
				}else{
					$info[$k]['show_status_value'] = '';	
				}
			}
			return $info;
		}else if( $type == 2 ){
			//日志收藏
			$info = Diary::findAll(array('id IN ({gid:array}) ','bind'=> array( 'gid'=>$gidArr )));
			return $info;
		}
    }
	
}
